﻿using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class ChannelConfigService : IChannelConfigService
{
    private readonly IOptions<Dictionary<string, ChannelConfig>> _options;

    public ChannelConfigService(IOptions<Dictionary<string, ChannelConfig>> options)
    {
        _options = options;
    }

    public ChannelConfig GetConfigForChannel(string channelId)
    {
        // 验证渠道ID格式
        if (string.IsNullOrWhiteSpace(channelId) ||
            !Regex.IsMatch(channelId, @"^[a-z0-9_]+$"))
        {
            channelId = "default";
        }

        // 获取配置
        var channels = _options.Value;

        // 尝试获取指定渠道配置
        if (channels.TryGetValue(channelId, out var config))
        {
            return config;
        }

        // 返回默认配置
        return channels.TryGetValue("default", out var defaultConfig)
            ? defaultConfig
            : new ChannelConfig();
    }
}